home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2219 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can't figure this out
  5. Date: 19 Jan 1996 22:27:54 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan19172754@g7240065.bridge.bst.bls.com>
  8. References: <31000091.3778302@news.panix.com>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: dm@panix.com's message of Fri, 19 Jan 1996 21:11:43 GMT
  11.  
  12. In article <31000091.3778302@news.panix.com> dm@panix.com (Dan'l) writes:
  13.  
  14. : I am learning C and I have not had any problems understanding most
  15. : concepts I have learned so far.  But to date I still can't figure out
  16. : how the outcome of this program is 15.  Somehow one of the B's ends up
  17. : a three and the other B a 5, or am I so off base that I can't see
  18. : what's really happening.    Can someone please walk me through this
  19. : one.                                             Thanks     Dan'l
  20.  
  21. : #define A 3
  22. : #define B A + A
  23. : #define C B * B
  24.  
  25. : main()
  26. : {
  27. :       printf("%d", C);
  28. :       return 0;
  29. : }
  30.  
  31. Expanding the #defines you get
  32.  
  33. #define A 3
  34. #define B A + A
  35. #define C B * B
  36.  
  37. expanding A
  38.  
  39. #define B 3 + 3
  40. #define C B * B
  41.  
  42. expanding B
  43.  
  44. #define C 3 + 3 * 3 + 3
  45.  
  46. As '*' has a higher precedence than '+' this is evaluated as
  47.  
  48.    3 + (3 * 3) + 3
  49.  
  50. Which is 15.
  51.  
  52. Regards
  53.  
  54.    -A.
  55.  
  56.   
  57. -- 
  58. | A.Champion                |
  59.